<template>
  <uni-collapse accordion>
    <uni-collapse-item v-for="item in careOrderList" :key="item.coId" :border="false" :show-arrow="false">
      <template v-slot:title>
        <uni-card>
          <view class="itemTitle">
            <view class="left">
              <view class="coId">订单号{{ item.coId }}</view>
              <view>创建时间：{{ item.createTime }}</view>
              <view>主治医生：{{ item.createBy }}</view>
            </view>
          </view>
        </uni-card>
      </template>
      <navigator
          :url="`/pagesMember/checkOrder/checkDetails?itemId=${items.itemId}`"
          class="content"
          v-for="(items, index) in item.careOrderItems"
          :key="items.itemId"
      >
        <view style="display: flex">
          <view>
            <text class="green">【检查处方】</text>
            <text>【{{ index + 1 }}】</text>
            <text>【处方总额】￥{{ items.amount }}</text>
          </view>
          <view style="margin-left: 50rpx;color: #ffb32c;font-weight: bolder;font-size: 30rpx">
            {{ items.status === '0' ? '检测中' : '检查完成' }}
          </view>
        </view>
        <view class="contentItem">
          <view>检查项目：{{ items.itemName }}</view>
          <view>备注信息：{{ items.remark }}</view>
          <view>项目价格：{{ items.price }}</view>
          <view>检查次数：{{ items.num }}</view>
        </view>
      </navigator>
    </uni-collapse-item>
  </uni-collapse>
</template>

<script setup lang="ts">
import { getCareOrderListByPatientIdAPI, getCheckOrderDetailsByItemIdAPI } from '@/services/docter/checkOrder';
import { ref } from 'vue';
import type { CareOrder, CheckOrderDetail } from '@/types/docter/careOrder';
import { onLoad } from '@dcloudio/uni-app';
import { useMemberStore } from '@/stores';

const memberStore = useMemberStore();
const profile = memberStore.profile;

const careOrderList = ref<CareOrder[]>([]);

const getCareOrderList = async () => {
  const res = await getCareOrderListByPatientIdAPI(profile.patientId);
  careOrderList.value = res.data;

  await Promise.all(
      careOrderList.value.map(async (order) => {
        if (order.careOrderItems) {
          const filteredItems = await Promise.all(
              order.careOrderItems.map(async (item) => {
                if (item.itemId) {
                  const checkRes = await getCheckOrderDetailsByItemIdAPI(item.itemId);
                  const detail = checkRes.data;

                  // 如果数据为空或者 itemId 不匹配，返回 null
                  if (!detail || detail.cocId !== item.itemId) {
                    return null;
                  }

                  // 更新 item 的状态
                  item.status = detail.resultStatus;
                  return item;
                }
              })
          );

          // 过滤掉无效的 careOrderItems
          order.careOrderItems = filteredItems.filter(item => item !== null);
        }
      })
  );

  // 过滤掉 careOrderItems 为空的订单
  careOrderList.value = careOrderList.value.filter(order => order.careOrderItems && order.careOrderItems.length > 0);
};

onLoad(() => {
  getCareOrderList();
});
</script>

<style scoped lang='scss'>
.itemTitle {
  display: flex;
  align-items: center;

  .left {
    flex: 1 1 0%;
    .coId {
      color: black;
      font-weight: bold;
      font-size: 30rpx;
    }
    view {
      margin: 5rpx 0;
      font-size: 26rpx;
      color: #999999;
    }
  }

  .right {
    flex: 1 1 0%;
  }
}

.content {
  padding: 20rpx;

  .contentItem {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;

    > view {
      width: 48%;
      padding: 5rpx 0;
      color: #999;
    }
  }

  view {
    .green {
      color: #00bf9c;
    }
  }
}
</style>
